package com.gr

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Company)
class CompanyTests {

	def company

	@Before
	void setUp() {
		// Make sure we can invoke validate() on our User domain object.
		mockForConstraintsTests(Company)
		// Set up default company, so we can easily test single properties.
		company = new Company(title: 'duinc', email: 'duinc@gmail.com', startDate: new Date("2001/1/18"))
	}

	/**
	 * 	title(unique: true)
	 */
	void testConstraintsTitleUnique() {

		def test = new Company(title: 'duinc', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		mockForConstraintsTests(Company, [test])

		assertFalse company.validate()
		assertEquals 'unique', company.errors['title']

		company = new Company(title: 'some', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		assertTrue company.validate()
	}

	/**
	 * title(blank: false)
	 */	
	void testConstraintsTitleBlank() {
		company = new Company(title: '', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		assertFalse company.validate()
		assertEquals 'blank', company.errors['title']

		company = new Company(title: 'notempty', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		assertTrue company.validate()
	}

	/**
	 * title(size: 2..50)
	 */
	void testConstraintsTitleSize() {
		company = new Company(title: 'd', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		assertFalse company.validate()
		assertEquals 'size', company.errors['title']

		company = new Company(title: '123456789012345678901234567890123456789012345678901', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		assertFalse company.validate()
		assertEquals 'size', company.errors['title']

		company = new Company(title: 'normalsize', email: 'some@gmail.com', startDate: new Date("2010/5/24"))
		assertTrue company.validate()
	}

	/**
	 * email(email: true)
	 */
	void testConstraintsEmailEmail() {
		company.email = 'somesting'
		assertFalse company.validate()
		assertEquals 'email', company.errors['email']

		company.email = 'valid@email.ru'
		assertTrue company.validate()
	}

	/**
	 * email(nullable: true)
	 */
	void testConstraintsEmailNullable() {
		company.email = ''
		assertTrue company.validate()
		assertNull company.errors['email']
	}

	/**
	 * startDate(nullable: true)
	 */
	void testConstraintsStartDateNullable() {
		company.startDate = null
		assertTrue company.validate()
		assertNull company.errors['startDate']
	}

}